﻿using Newtonsoft.Json.Bson;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using WinFormsApp1.Common;
using static WinFormsApp1.TongShiWei.PigData.DepotData;
using static WinFormsApp1.TongShiWei.PigData.PigWeighData;

namespace WinFormsApp1.TongShiWei.PigData
{
    public partial class PigWeighData : UIForm
    {
        List<UIRadioButton> uIRadioButtons_ = new List<UIRadioButton>();
        public PigWeighData(string? WeighTime)
        {
            InitializeComponent();
            if (WeighTime != null)
            {
                this.uiTextBox5.Text = WeighTime;
            }
            ShowScaleGoodsData();
            this.uiRadioButton1.Click += uiRadioButton5_CheckedChanged;
            this.uiRadioButton2.Click += uiRadioButton5_CheckedChanged;
            this.uiRadioButton3.Click += uiRadioButton5_CheckedChanged;
            this.uiRadioButton4.Click += uiRadioButton5_CheckedChanged;
            this.uiRadioButton5.Click += uiRadioButton5_CheckedChanged;
            uIRadioButtons_.Add(uiRadioButton1);
            uIRadioButtons_.Add(uiRadioButton2);
            uIRadioButtons_.Add(uiRadioButton3);
            uIRadioButtons_.Add(uiRadioButton4);
            uIRadioButtons_.Add(uiRadioButton5);
            GetDepotData();
            ShowWhitePorkData_();
        }
        #region 获取仓库表数据
        DepotData_ depotData_ = new DepotData_();
        bool IsDepotData = false;
        public async void GetDepotData()
        {
            //http://localhost:5106/api/Tsw/ShowDepotData
            var res = await HttpClientHelper.RequestUrl<DepotData_, ApiResult<List<DepotData_>>>("http://localhost:5106/api/Tsw/", "ShowDepotData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                var DepotData = res.Data.FirstOrDefault(x => x.DepotTypeId == DepotId_);
                if (DepotData != null)
                {
                    if (IsDepotData == true)
                    {
                        depotData_ = DepotData;
                        IsDepotData = false;
                    }
                }
            }
        }
        #endregion
        #region 获取磅秤表信息
        ScaleData_ scaleData_ = new ScaleData_();
        public async void ShowScaleData()
        {
            //localhost:5106/api/Tsw/ShowScaleData
            var res = await HttpClientHelper.RequestUrl<ScaleData_, ApiResult<List<ScaleData_>>>("http://localhost:5106/api/Tsw/", "ShowScaleData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                var data_ = res.Data.FirstOrDefault(x => x.ScaleTypeId == ScaleId_);
                if (data_ != null)
                {
                    scaleData_ = data_;
                    ScalePNum.Text = data_.TareNum.ToString();
                }
            }
        }
        #endregion
        int kid = 0;
        List<Button> buttons = new List<Button>();
        public async void ShowScaleGoodsData(int pid = 0)
        {
            List<ScaleGoods> scaleGoods = new List<ScaleGoods>()
        {
            new ScaleGoods{ ScaleGoodsId=1,ScaleGoodsTypeName="带皮白条",ScaleGoodsName="带皮一级白条",ScaleGoodsKg=108.32},
            new ScaleGoods{ ScaleGoodsId=2,ScaleGoodsTypeName="带皮白条",ScaleGoodsName="带皮二级白条",ScaleGoodsKg=105},
            new ScaleGoods{ ScaleGoodsId=3,ScaleGoodsTypeName="带皮白条",ScaleGoodsName="带皮三级白条",ScaleGoodsKg=100.32},
            new ScaleGoods{ ScaleGoodsId=4,ScaleGoodsTypeName="带皮白条",ScaleGoodsName="带皮等外白条",ScaleGoodsKg=95},

            new ScaleGoods{ ScaleGoodsId=5,ScaleGoodsTypeName="去皮白条",ScaleGoodsName="去皮一级白条",ScaleGoodsKg=110.33},
            new ScaleGoods{ ScaleGoodsId=6,ScaleGoodsTypeName="去皮白条",ScaleGoodsName="去皮二级白条",ScaleGoodsKg=130.37},

            new ScaleGoods{ ScaleGoodsId=7,ScaleGoodsTypeName="去蹄带皮白条",ScaleGoodsName="去蹄带皮一级白条",ScaleGoodsKg=95},
            new ScaleGoods{ ScaleGoodsId=8,ScaleGoodsTypeName="去蹄带皮白条",ScaleGoodsName="去蹄带皮二级白条",ScaleGoodsKg=110.33},
            new ScaleGoods{ ScaleGoodsId=9,ScaleGoodsTypeName="去蹄带皮白条",ScaleGoodsName="去蹄带皮三级白条",ScaleGoodsKg=130.37},
        };
            if (pid > 0 && buttons.Count > 0)
            {
                foreach (var item in buttons)
                {
                    this.Controls.Remove(item);
                }
                buttons.Clear();
            }

            switch (pid)
            {
                case 1:
                    scaleGoods = scaleGoods.Where(x => x.ScaleGoodsTypeName == "带皮白条").ToList();
                    break;
                case 2:
                    scaleGoods = scaleGoods.Where(x => x.ScaleGoodsTypeName == "去皮白条").ToList();
                    break;
                case 3:
                    scaleGoods = scaleGoods.Where(x => x.ScaleGoodsTypeName == "去蹄带皮白条").ToList();
                    break;
            }
            int x = 220; int y = 280; int i = 1;
            foreach (var item in scaleGoods)
            {
                Button button = new Button()
                {
                    Location = new Point(x, y),
                    Size = new System.Drawing.Size(150, 80),
                    AutoSize = true,
                    Name = item.ScaleGoodsKg.ToString(),
                    //TextAlign = ContentAlignment.MiddleCenter,
                    BackColor = Color.White,
                };
                button.Text = item.ScaleGoodsName;
                button.AccessibleName = item.ScaleGoodsTypeName;
                button.Click += new System.EventHandler(Button_Cliek);
                this.Controls.Add(button);
                buttons.Add(button);
                if (i == 8)
                {
                    x = 220;
                    y += 90;
                    i = 1;
                }
                else
                {
                    x += 165;
                    i++;
                }
            }

        }
        string DepotypeName = "";
        public void Button_Cliek(object sender, EventArgs e)
        {
            var data = sender as Button;
            var ButtonName = data.Name;
            DepotypeName = data.AccessibleName;
            var scalejnum = Convert.ToDouble(ButtonName) - Convert.ToDouble(ScalePNum.Text);
            ScaleZNum.Text = (Convert.ToDouble(ButtonName) * Convert.ToInt32(this.GoodsNum.Text)).ToString();
            ScaleJNum.Text = scalejnum.ToString();
        }
        public class ScaleGoods
        {
            public int ScaleGoodsId { get; set; }
            public string ScaleGoodsTypeName { get; set; }
            public string ScaleGoodsName { get; set; }
            public double ScaleGoodsKg { get; set; }
        }
        #region 商品分类查询
        private void uiButton6_Click(object sender, EventArgs e)
        {
            ShowScaleGoodsData(0);
        }
        private void uiButton5_Click(object sender, EventArgs e)
        {
            ShowScaleGoodsData(1);
        }

        private void uiButton8_Click(object sender, EventArgs e)
        {
            ShowScaleGoodsData(2);
        }

        private void uiButton13_Click(object sender, EventArgs e)
        {
            ShowScaleGoodsData(3);
        }

        #endregion
        #region 划膘单选框
        string HuaBiao_ = "";

        private void HuaBiao_CheckedChanged(object sender, EventArgs e)
        {
            HuaBiao.BackColor = Color.DodgerBlue;
            HuaBiao.ForeColor = Color.Wheat;
            var data = sender as UIRadioButton;
            HuaBiao_ = data.Text;
            if (data.Checked)
            {
                this.HuaBiao2.Checked = false;
                this.HuaBiao2.BackColor = Color.White;
                this.HuaBiao2.ForeColor = Color.Black;
            }
            return;
        }

        private void HuaBiao2_CheckedChanged(object sender, EventArgs e)
        {
            HuaBiao2.BackColor = Color.DodgerBlue;
            HuaBiao2.ForeColor = Color.Wheat;
            var data = sender as UIRadioButton;
            HuaBiao_ = data.Text;
            if (data.Checked)
            {
                this.HuaBiao.Checked = false;
                this.HuaBiao.BackColor = Color.White;
                this.HuaBiao.ForeColor = Color.Black;
            }
            return;
        }
        #endregion

        #region 扣减项单选框
        string DeductionName_ = "";
        private void uiRadioButton5_CheckedChanged(object sender, EventArgs e)
        {
            var data = sender as UIRadioButton;
            DeductionName_ = data.Text;
            if (data.Checked)
            {
                data.BackColor = Color.DodgerBlue;
                data.ForeColor = Color.White;
            }
            var UiRaidoButtonList = uIRadioButtons_.Where(x => x.Name != data.Name).ToList();
            foreach (var item in UiRaidoButtonList)
            {
                item.BackColor = Color.WhiteSmoke;
                item.ForeColor = Color.Black;
            }
            var name = data.Text;
        }
        #endregion

        #region 添加白条称重明细表
        private async void uiButton20_Click(object sender, EventArgs e)
        {
            var DepotNameData = "";
            var res_ = await HttpClientHelper.RequestUrl<DepotData_, ApiResult<List<DepotData_>>>(URLCommon.Url2, "/TSW/ShowDepotData", HttpClientHelper.MethodType.GET);
            if (res_.Code > 0)
            {
                var data = res_.Data.FirstOrDefault(x => x.DepotTypeId == scaleData_.BindDepotId);
                DepotNameData = data.DepotTypeName;
            }
            //获取选择的仓库信息
            WhiteWeighing whiteWeighing = new WhiteWeighing()
            {
                WhiteWeighingId = 0,
                DepotName = DepotNameData,
                SlaughterNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
                GoodsMinute = DepotypeName,
                WhiteWeighingDate = DateTime.Parse(this.uiTextBox5.Text),
                GrossWeight = this.ScaleZNum.Text.ToDouble(),
                SkinWeight = this.ScalePNum.Text.ToDouble(),
                NetWeight = this.ScaleJNum.Text.ToDouble(),
                DeductionName = DeductionName_,
                WhiteWeighingDesc = uiTextBox17.Text,
                WeighingNum = Convert.ToInt32(this.GoodsNum.Text),
                FattenName = HuaBiao_,
                IsDel = false
            };
            if (whiteWeighing != null)
            {
                //http://localhost:5159/api/TSW/AddWhitePorkData
                var res = await HttpClientHelper.RequestUrl<WhiteWeighing, ApiResult<WhiteWeighing>>(URLCommon.Url1, "/TSW/AddWhitePorkData", HttpClientHelper.MethodType.POST, whiteWeighing);
                if (res.Code > 0)
                {
                    MessageBox.Show("添加成功");
                    WhitePorkList.Rows.Clear();
                    ShowWhitePorkData_();
                    return;
                }
                else
                {
                    MessageBox.Show("添加失败");
                    return;
                }
            }
        }
        #endregion
        #region 获取仓库值
        /// <summary>
        /// 显示仓库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        int DepotId_ = 0;
        private void uiButton2_Click(object sender, EventArgs e)
        {
            IsDepotData = true;
            DepotData data = new DepotData();
            data.GetDepotIdData += GetDepotId;
            data.ShowDialog();
        }
        public void GetDepotId(int Depotid_)
        { 
            DepotId_= Depotid_;
        }
        #endregion
        #region 获取磅秤值
        int ScaleId_ = 0;
        private void uiButton1_Click(object sender, EventArgs e)
        {
            ScaleData data = new ScaleData(DepotId_);
            data.GetScaleIdData += GetScaleId;
            data.Show();
        }
        public void GetScaleId(int ScaleId)
        {
            ScaleId_ = ScaleId;
            ShowScaleData();
        }
        #endregion

        #region 配置类表
        /// <summary>
        /// 白条称重明细表
        /// </summary>
        public class WhiteWeighing
        {
            /// <summary>
            /// 白条明细表ID
            /// </summary>
            public int WhiteWeighingId { get; set; }
            /// <summary>
            /// 仓库
            /// </summary>
            public string DepotName { get; set; }
            /// <summary>
            /// 屠宰编号
            /// </summary>
            public string SlaughterNo { get; set; }
            /// <summary>
            /// 商品代号
            /// </summary>
            public string GoodsMinute { get; set; }
            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime WhiteWeighingDate { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public double SkinWeight { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public double GrossWeight { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public double NetWeight { get; set; }
            /// <summary>
            /// 扣减项
            /// </summary>
            public string DeductionName { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string WhiteWeighingDesc { get; set; }
            /// <summary>
            /// 数量
            /// </summary>
            public int WeighingNum { get; set; }
            /// <summary>
            /// 划膘编号
            /// </summary>
            public string FattenName { get; set; }
            /// <summary>
            /// 逻辑删除
            /// </summary>
            public bool IsDel { get; set; }
        }
        public class ScaleData_
        {
            /// <summary>
            /// 磅秤表ID
            /// </summary>
            public int ScaleId { get; set; }
            /// <summary>
            /// 磅秤类型ID
            /// </summary>
            public int ScaleTypeId { get; set; }
            /// <summary>
            /// 磅秤名称
            /// </summary>
            public string ScaleName { get; set; } = string.Empty;
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool IsScaleState { get; set; }
            /// <summary>
            /// 皮重扣减(公斤)
            /// </summary>
            public int TareNum { get; set; }
            /// <summary>
            /// 皮重按件累加
            /// </summary>
            public bool IsTareAdd { get; set; } = false;
            /// <summary>
            /// 绑定仓库
            /// </summary>
            public int BindDepotId { get; set; }
            /// <summary>
            /// 自动出/入库
            /// </summary>
            public bool IsGoDepot { get; set; } = false;
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
            /// <summary>
            /// 操作人
            /// </summary>
            public string OperatorName { get; set; } = string.Empty;
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime ScaleCreateTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 最后修改人
            /// </summary>
            public string Modifier { get; set; } = string.Empty;
            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime RevempTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 是否删除{false:不删除,true:删除}
            /// </summary>
            public bool IsScaleDelete { get; set; } = false;
            /// <summary>
            /// 是否禁用{false:不禁用,true:禁用}
            /// </summary>
            public bool IsScaleDisable { get; set; } = false;
        }
        public class DepotData_
        {
            /// <summary>
            /// 仓库类型ID
            /// </summary>
            public int DepotTypeId { get; set; }
            /// <summary>
            /// 仓库类型名称
            /// </summary>
            public string DepotTypeName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除仓库类型{false=不删除；true=删除}
            /// </summary>
            public bool IsDepotType { get; set; }
        }
        #endregion
        #region 显示白条称重表格数据
        int AddNum = 0;
        public async void ShowWhitePorkData_()
        {
            List<WhiteWeighing> whiteWeighings = new List<WhiteWeighing>();
            //http://localhost:5159/api/TSW/ShowWhitePorkData
            var res = await HttpClientHelper.RequestUrl<WhiteWeighing, ApiResult<List<WhiteWeighing>>>(URLCommon.Url2, "/TSW/ShowWhitePorkData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                foreach (var item in res.Data)
                {
                    WhitePorkList.Rows.Add(item.WhiteWeighingId, item.DepotName, item.SlaughterNo, item.WhiteWeighingDate.ToString("yyyy-MM-dd HH:mm:ss"), item.GoodsMinute, item.WeighingNum, item.GrossWeight, item.SkinWeight, item.NetWeight, item.DeductionName, item.WhiteWeighingDesc);
                    if (item.FattenName == "大划")
                    {
                        this.Num004.Text = (Convert.ToInt32(this.Num004.Text) + 1).ToString();
                    }
                    else if (item.FattenName == "中划")
                    {
                        this.Num005.Text = (Convert.ToInt32(this.Num005.Text) + 1).ToString();
                    }
                    else
                    {
                        this.Num001.Text = (Convert.ToInt32(this.Num001.Text) + 1).ToString();
                        this.Num002.Text = (Convert.ToInt32(this.Num002.Text) + 1).ToString();
                        this.Num003.Text = (Convert.ToInt32(this.Num003.Text) + 1).ToString();
                    }
                }
                if (AddNum == 0)
                {
                    DataGridViewButtonColumn DelButton = new DataGridViewButtonColumn();
                    DelButton.Text = "作废";
                    DelButton.UseColumnTextForButtonValue = true;
                    DataGridViewButtonColumn UpButton = new DataGridViewButtonColumn();
                    UpButton.Text = "修改";
                    UpButton.UseColumnTextForButtonValue = true;
                    WhitePorkList.CellClick += WhiteProClick;
                    WhitePorkList.Columns.AddRange(DelButton, UpButton);
                    AddNum = 1;
                }

            }
        }
        #endregion

        /// <summary>
        /// 白条称重明细表表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region 白条称重明细表删除和修改
        public async void WhiteProClick(object sender, DataGridViewCellEventArgs e)
        {
            DelOrUpData delOrUpData = new DelOrUpData()
            {
                WhiteWeighingId = Convert.ToInt32(WhitePorkList.Rows[e.RowIndex].Cells[0].Value)
            };
            if (e.ColumnIndex == 11)
            {
                //http://localhost:5159/api/TSW/DelWhitePorkData
                var res = await HttpClientHelper.RequestUrl<DelOrUpData, ApiResult<DelOrUpData>>(URLCommon.Url1, "/TSW/DelWhitePorkData", HttpClientHelper.MethodType.POST, delOrUpData);
                if (res.Code > 0)
                {
                    WhitePorkList.Rows.Clear();
                    MessageBox.Show("删除成功");
                    ShowWhitePorkData_();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else if (e.ColumnIndex == 12)
            {
                UpWhitePork upWhitePork = new UpWhitePork(delOrUpData.WhiteWeighingId);
                upWhitePork.Show();
            }
            else
            {
                return;
            }
        }
        #endregion
        private record DelOrUpData
        {
            public int WhiteWeighingId { get; set; }
        }
        private void WhitePorkList_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
